package com.feri.gpstudy.service.impl;

import com.feri.gpstudy.bo.NurseAddBo;
import com.feri.gpstudy.bo.NurseWhereBo;
import com.feri.gpstudy.dao.NurseDao;
import com.feri.gpstudy.entity.Nurse;
import com.feri.gpstudy.service.intf.NurseService;
import com.feri.gpstudy.vo.R;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class NurseServiceImpl implements NurseService {

    /**
     * 注入持久层对象
     */
    @Resource
    private NurseDao dao;
    /**
     * 新增方法
     * @param bo
     * @return
     */
    @Override
    public R save(NurseAddBo bo) {
        // 1.查询 用户名是否存在
        if (dao.selectById(bo.getNid()) == null) {
            // 组装名对象
            Nurse nurse = new Nurse();
            BeanUtils.copyProperties(bo,nurse);
            // 操作数据库
            if (dao.insert(nurse) > 0) {
                return R.ok();
            }
        }
        return R.fail("新增异常！");

    }

    @Override
    public R add(Nurse nurse) {
        if (dao.insert(nurse) > 0) {
            return  R.ok(nurse);
        }
        return null;
    }

    /**
     * 修改内容
     * @param nurse
     * @return
     */
    @Override
    public R update(Nurse nurse) {
        if (nurse != null) {
            int i = 0;
            i = dao.update(nurse);
            if (i > 0) {
                return R.ok();
            }
        }
        return R.fail("新增失败");
    }

    /**
     * 查询详情
     * @param nid
     * @return
     */
    @Override
    public R queryById(Integer nid) {
        if (nid != null) {
            Nurse nurse = new Nurse();
            nurse = dao.selectById(nid);
            if (nurse != null) {
                return R.ok(nurse);
            }
        }
        return R.fail("查无此人");
    }

    /**
     * 查询所有数据
     * @param bo
     * @return
     */
    @Override
    public R queryAll(NurseWhereBo bo) {
        PageHelper.startPage(bo.getPage(), bo.getSize());
        PageInfo<Nurse> pageInfo = new PageInfo<>(dao.selectAll(bo.getName(),bo.getTitle(),bo.getPhone()));
        return R.ok(pageInfo);
    }
}
